import { getAuthCode, getUserInfo } from '@/utils/login';
import {
  pushMeber,
  tokenByCodeForAli,
  wxJsCode,
} from '@/api/login';
import { getLoginInfoFn } from '@/common/common'

export const loginMixin = {
  data() {
    return {
      code: '',
      branchData: '',
    }
  },
  methods: {
    bindMember() {
      const params = {
        loginAccount: this.vuex_accountInfo.loginAccount,
        inviteCode: uni.getStorageSync('inviteCode')
      }
      pushMeber(params).then((res) => {
        console.log(res)
      })
    },
    loginEvent(branchData, pageType, callbackUrl = '', urlParams = null) {
      this.branchData = branchData
      this.getAccountFn().then(() => {
        this.getUserInfoFn(branchData, pageType, callbackUrl, urlParams);
      })
    },
    /*获取账号参数*/
    getAccountFn() {
      return new Promise((resolve) => {
        const provider = this.vuex_provider;
        getAuthCode(provider, (loginRes) => {
          if (!loginRes.code) {
            this.$u.toast('获取code失败')
            return
          }
          this.$u.vuex('vuex_login_code', loginRes.code)
          this.code = loginRes.code;
          // #ifdef MP-ALIPAY
          resolve();
          // #endif
        })
        // #ifdef MP-WEIXIN
        resolve();
        // #endif
      })
    },
    /*获取用户信息*/
    getUserInfoFn(branchData, pageType, callbackUrl, urlParams) {
      if (branchData) {
        var {
          branchId,
          branchName,
        } = branchData
      }
      getUserInfo((res) => {
        this.$u.vuex('vuex_userInfo', res)
        const params = {
          code: this.code,
          branchId: branchId || null, //销售公司id
          branchName: branchName || null, //销售公司名称
        }
        // #ifdef MP-WEIXIN
        wxJsCode(params).then((res) => {
          this.$u.vuex('vuex_accountInfo', res.data) //res.data
          if (res.errorCode == 0) {
            this.$nextTick(() => {
              getLoginInfoFn(res.data.memberId)
              this.$u.vuex('vuex_loginStatus', true)
              this.bindMember()
              this.afterLoginHandleFn(pageType, callbackUrl, urlParams)
            })
          }
          if (res.errorCode == 2) {
            this.pageType = pageType
            this.$u.vuex('vuex_accountInfo', res.data)
            this.showPopVisible = true
          }
        }).catch(() => {
          // eslint-disable-next-line no-mixed-spaces-and-tabs
        })
        // #endif
        // #ifdef MP-ALIPAY
        tokenByCodeForAli(params).then((res) => {
          this.$u.vuex('vuex_accountInfo', res.data)
          if (res.errorCode == 0) {
            this.$nextTick(() => {
              getLoginInfoFn(res.data.memberId)
              this.$u.vuex('vuex_loginStatus', true)
              this.bindMember()
              this.afterLoginHandleFn(pageType, callbackUrl, urlParams)
            })
          }
          if (res.errorCode == 2) {
            this.pageType = pageType
            this.$u.vuex('vuex_accountInfo', res.data)
            this.showPopVisible = true
          }
          if (res.errorCode == 1) {
            this.$u.toast(res.msg)
          }
        }).catch(() => {
          // eslint-disable-next-line no-mixed-spaces-and-tabs
        })
        // #endif
      }, '完善客户资料')
    },
    afterLoginHandleFn(pageType, callbackUrl, urlParams) {
      let paramStr = ''
      if (urlParams) {
        for (const urlParamsKey in urlParams) {
          paramStr += urlParamsKey + '=' + urlParams[urlParamsKey] + '&'
        }
      }
      /*根据账号类型跳转对应首页
      * 账户类型：10:签约用户；20:普通用户;30认证用户;39普通会员-分销;40分销用户；*/
      if (!this.callbackUrl && this.vuex_loginStatus && this.vuex_memberInfo && (this.vuex_memberInfo.accountType == 20 || this.vuex_memberInfo.accountType == 30)) {
        this.callbackUrl = '/shopIndex/shopIndex'
      } else if (this.callbackUrl.includes('purchaseOrder/index') && this.vuex_memberInfo.accountType == 10) {
        this.callbackUrl = '/pages/index/index'
      } else {
        !this.callbackUrl ? this.callbackUrl = '/pages/index/index' : ''
      }
      if (pageType == 'pay') {
        this.paymentFn ? this.paymentFn() : ''
      } else if (pageType == 'order') {
        uni.redirectTo({ url: callbackUrl })
      } else if (pageType == 'my') {
        uni.redirectTo({ url: callbackUrl })
      } else if (pageType == 'ground') {
        uni.redirectTo({ url: callbackUrl })
      } else {
        uni.redirectTo({ url: callbackUrl + '?' + paramStr })
      }
      // if (pageType == 'confirm') {
      // this.confirmOrderFn ? this.confirmOrderFn() : ''
      // }
      // if (pageType == 'order' && this.orderStatusClick) {
      //   this.orderStatusClick ? this.orderStatusClick([]) : ''
      // }
    },
    closePop() {
      this.showPopVisible = false
    },
  }
}
